<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * materialout.php
 * @author ranuandra
 *
 * 17 Agu 2011 23:05:32
 *
 * 
 */
class Controller_Materialout extends Kohana_Mapi
{
	
	public function action_index()
	{
		$this->auto_render = false;
		echo "";
	}
	public function action_view()
	{
		$this->auto_render = false;

		$materialin = ORM::factory('materialout')->find_all();
		$hasil = array();

		foreach($materialin as $data)
		{
			$datas = array(
				'no_order'  => $data->no_order,
				'tgl'		=> $data->tgl,
			    'id'		=> $data->id,
				'no_charge'	=> $data->no_charge,
				'group'		=> $data->detailmaterialout->group->name,
			);
			$hasil[] = $datas;
		}
		echo json_encode(array("success" => true, "message" => "Loaded data", "materialout" => $hasil));		
	}
	
	public function action_save()
	{
		$this->auto_render = false;
		if(isset($_POST))
		{

			try {
				//$post = Validation::factory($_POST);
					$jam = date("H:m:s");
							
					list($d, $m, $y) = preg_split('/\//', $_POST['tgl']);
							
					$tgl = $y."-".$m."-".$d." ".$jam;
					
					$_POST['tgl'] = $tgl;
					
					$materialout = ORM::factory('materialout');
					$materialout->create_materialout($_POST);
					
					$datas = json_decode(stripcslashes($_POST['material']), true);
					
					$bulan = (int)$m;

					$dataarray = false;
					$single = array();
					$output = array();
					$hasil = array();
			
						foreach($datas as $data)
						{
							if (is_array($data)) 
							{
								try {
								//$post = Validation::factory($_POST);
									$dmaterial = ORM::factory('detailmaterialout');
									$dmaterial->materialout_id = $materialout->id;
									$dmaterial->create_detailmaterialout($data);
									//Request::current()->redirect(URL::base().'customer');
									//array_push($array, $var)
									//$single = array_merge(array("id"=>$location->id), $data);
									//array_push($single, $data);
									//array_push($hasil, $single);
									//echo json_encode(array("success" => true, "message" => "Loaded data", "ratebeli" => $hasil));

									$stock_bln_ini = ORM::factory('stock')->where('material_id', '=', $data['material_id'])->where('bulan', '=', $bulan)->where('tahun', '=', $y)->where('location_id', '=', $data['location_id'])->find();
									
									if($stock_bln_ini->id != "") //bila bulan ini ada stock maka
									{
										$jmlstock = $stock_bln_ini->jumlah - $data['jumlah'];
										$stock_akhir = ORM::factory('stock', $stock_bln_ini->id);
										$stock_akhir ->jumlah = $jmlstock;
										$stock_akhir ->save();
										
									}
									else //bila bulan ini tdk ada stock maka cek stock bulan lalu
									{
										$bulanlalu = $bulan-1;
										if($bulanlalu == 0)
										{
											$bulanlalu = 12;
											$ylalu = $y-1;
										}
										
										if(isset($ylalu))
										{
											$stock_bln_lalu = ORM::factory('stock')->where('material_id', '=', $data['material_id'])->where('bulan', '=', $bulanlalu)->where('tahun', '=', $ylalu)->where('location_id', '=', $data['location_id'])->find();
										}
										else 
										{
											$stock_bln_lalu = ORM::factory('stock')->where('material_id', '=', $data['material_id'])->where('bulan', '=', $bulanlalu)->where('tahun', '=', $y)->where('location_id', '=', $data['location_id'])->find();
										}
										//echo $stock_bln_lalu->id ;
										//exit;
										if($stock_bln_lalu->id != "") //bila bulan lalu ada stock maka tambahkan dan simpan
										{
											$jmlstock = $stock_bln_lalu->jumlah - $data['jumlah'];
											$stock_akhir = ORM::factory('stock');
											$stock_akhir->jumlah = $jmlstock;
											$stock_akhir->bulan = $bulan;
											$stock_akhir->tahun = $y;
											$stock_akhir->location_id = $data['location_id'];
											$stock_akhir->material_id = $data['material_id'];
											$stock_akhir->save();
											
										}
										else  //bila bulan lalu tidak ada stock maka masukkan stock yg ada 
										{
											$jmlstock = $data['jumlah'];
											$stock_akhir = ORM::factory('stock');
											$stock_akhir->jumlah = $jmlstock;
											$stock_akhir->bulan = $bulan;
											$stock_akhir->tahun = $y;
											$stock_akhir->location_id = $data['location_id'];
											$stock_akhir->material_id = $data['material_id'];
											$stock_akhir->save();										
										}
									}
				
								}
								catch(ORM_Validation_Exception $e)
								{
									$errors = $e->errors('models');
									print_r($errors);
								}
								$dataarray = true;
							}
			
						}	
						if($dataarray == false)
						{
								try {
								//$post = Validation::factory($_POST);
									$dmaterial = ORM::factory('detailmaterialout');
									$dmaterial->materialout_id = $materialout->id;
									$dmaterial->create_detailmaterialout($datas);
									//Request::current()->redirect(URL::base().'customer');
									//array_push($array, $var)
									//$single = array_merge(array("id"=>$location->id), $datas);
									//array_push($single, $data);
									//array_push($hasil, $single);
									//echo json_encode(array("success" => true, "message" => "Loaded data", "ratebeli" => $hasil));
																	$stock_bln_ini = ORM::factory('stock')->where('material_id', '=', $data['material_id'])->where('bulan', '=', $bulan)->where('tahun', '=', $y)->where('location_id', '=', $data['location_id'])->find();
									
									if($stock_bln_ini->id != "") //bila bulan ini ada stock maka
									{
										$jmlstock = $stock_bln_ini->jumlah - $datas['jumlah'];
										$stock_akhir = ORM::factory('stock', $stock_bln_ini->id);
										$stock_akhir ->jumlah = $jmlstock;
										$stock_akhir ->save();
										
									}
									else //bila bulan ini tdk ada stock maka cek stock bulan lalu
									{
										$bulanlalu = $bulan-1;
										if($bulanlalu == 0)
										{
											$bulanlalu = 12;
											$ylalu = $y-1;
										}
										if(isset($ylalu))
										{
											$stock_bln_lalu = ORM::factory('stock')->where('material_id', '=', $datas['material_id'])->where('bulan', '=', $bulanlalu)->where('tahun', '=', $ylalu)->where('location_id', '=', $datas['location_id'])->find();
										}
										else 
										{
											$stock_bln_lalu = ORM::factory('stock')->where('material_id', '=', $datas['material_id'])->where('bulan', '=', $bulanlalu)->where('tahun', '=', $y)->where('location_id', '=', $datas['location_id'])->find();
										}
								
										if($stock_bln_lalu->id != "") //bila bulan lalu ada stock maka tambahkan dan simpan
										{
											$jmlstock = $stock_bln_lalu->jumlah - $datas['jumlah'];
											$stock_akhir = ORM::factory('stock');
											$stock_akhir->jumlah = $jmlstock;
											$stock_akhir->bulan = $bulan;
											$stock_akhir->tahun = $y;
											$stock_akhir->location_id = $data['location_id'];
											$stock_akhir->material_id = $data['material_id'];
											$stock_akhir->save();
											
										}
										else  //bila bulan lalu tidak ada stock maka masukkan stock yg ada 
										{
											$jmlstock = $datas['jumlah'];
											$stock_akhir = ORM::factory('stock');
											$stock_akhir->jumlah = $jmlstock;
											$stock_akhir->bulan = $bulan;
											$stock_akhir->tahun = $y;
											$stock_akhir->location_id = $datas['location_id'];
											$stock_akhir->material_id = $datas['material_id'];
											$stock_akhir->save();										
										}
										
									}				
								}
								catch(ORM_Validation_Exception $e)
								{
									$errors = $e->errors('models');
									print_r($errors);
								}				
						}
						echo json_encode(array("success" => true, "message" => "Data Berhasil Disimpan"));

				}
				catch(ORM_Validation_Exception $e)
				{
					$errors = $e->errors('models');
					print_r($errors);
				}
			
		}
		else
		{
			Request::current()->redirect(URL::base().'customer/new');	
		}		
	}
}